# CMakeLists.txt
# This is the CMakeLists.txt for building the main application binary.
#-------------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-------------------------------------------------------------------------------


#-------------------------------------------------------------------------------
# Force Out of Source Build.
# We don't allow in-source builds. This causes no end of troubles because all
# out-of-source builds will use the CMakeCache.txt file there and even build
# the libraries and objects in it.
#-------------------------------------------------------------------------------
if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
  if (NOT DEFINED WITH_IN_SOURCE_BUILD)
    message(FATAL_ERROR
      "CMake builds are not not allowed within the source directory!"
      "\n Remove the CMakeCache.txt file and try again from another folder."
      "\n "
    )
  endif()
endif()


#-------------------------------------------------------------------------------
# CMake Compatibility
#-------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8)   # The minimum required of CMake supported.


#-------------------------------------------------------------------------------
# Project Details
#-------------------------------------------------------------------------------
project(code-sheriff CXX)             # Project name, source type.
set(VERSION_MAJOR 0)  # Project major version.
set(VERSION_MINOR 1)  # Project minor version.


#-------------------------------------------------------------------------------
# Discover the Operating System
#-------------------------------------------------------------------------------

# Initially assume no operating system.
set(OS_IS_LINUX   DISABLED)
set(OS_IS_APPLE   DISABLED)
set(OS_IS_WINDOWS DISABLED)

# Find the operating system we are building on.
if (UNIX)
  message(STATUS "Setting to build for UNIX." )
  set(OS_IS_LINUX ENABLED)
elseif (WINDOWS)
  message(STATUS "Setting to build for WINDOWS." )
  set(OS_IS_WINDOWS ENABLED)
else ()
  message(STATUS "Setting to build for APPLE." )
  set(OS_IS_APPLE ENABLED)
endif()


#-------------------------------------------------------------------------------
# Project Options
#-------------------------------------------------------------------------------
option(BUILD_PLUGINS "Set to ON to build plugins" ON)
set(WITH_BOOST ON)                    # TODO: make an option?


#-------------------------------------------------------------------------------
# TOOD: conditional debug symbols
#-------------------------------------------------------------------------------
set(CMAKE_CXX_FLAGS "-g -Wall")


#-------------------------------------------------------------------------------
# Platform Details
#-------------------------------------------------------------------------------

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package( Boost 1.46.0 COMPONENTS filesystem system)


if(Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
#  add_executable(foo foo.cc)
#  target_link_libraries(foo ${Boost_LIBRARIES})
endif()


#   if(WITH_BOOST)
#   
#     message(STATUS "BUILDING WITH BOOST" )
#   
#     # Search for the Boost libraries.
#     
#     
#     find_package( Boost 1.46 COMPONENTS filesystem system)
#     mark_as_advanced(Boost_DIR)  # why doesnt boost do this? TODO: what is this?
#     link_directories ( ${Boost_LIBRARY_DIRS} )
#     include_directories ( ${Boost_INCLUDE_DIRS} )
#     
#     set(BOOST_INCLUDE_DIR ${Boost_INCLUDE_DIRS})
#     set(BOOST_LIBRARIES ${Boost_LIBRARIES})
#     set(BOOST_LIBPATH ${Boost_LIBRARY_DIRS})
#     set(BOOST_DEFINITIONS "-DBOOST_ALL_NO_LIB")
#   endif()


# TODO: remove
#    # (unix defaults to OpenMP On)
#    if (UNIX AND NOT APPLE)
#    #  set(Boost_USE_MULTITHREADED OFF)
#      set(HAVE_PTHREAD_H 1)
#      set(HAVE_STDINT_H 1)
#      set(HAVE_TIME_H 1)
#    #  option(WITH_OPENMP        "Enable OpenMP (has to be supported by the compiler)" ON)
#    else()
#      set(HAVE_PTHREAD_H 0)
#      set(HAVE_STDINT_H 0)
#    #  option(WITH_OPENMP        "Enable OpenMP (has to be supported by the compiler)" OFF)
#    endif()


#-------------------------------------------------------------------------------
# Library Headers
#-------------------------------------------------------------------------------
include_directories("${PROJECT_SOURCE_DIR}/Source/External/Pluma/include")
include_directories("${PROJECT_SOURCE_DIR}/Source/Interfaces")


#-------------------------------------------------------------------------------
# Application Headers
#-------------------------------------------------------------------------------
set(INC
  "${PROJECT_SOURCE_DIR}/Source"
  "${PROJECT_SOURCE_DIR}/Source/System"
  "${PROJECT_BINARY_DIR}/Source/System"
  "${PROJECT_SOURCE_DIR}/Source/Configuration"
  "${PROJECT_SOURCE_DIR}/Source/Interfaces"
  "${PROJECT_SOURCE_DIR}/Source/Providers"
)

if (UNIX)
  set(INC
    "${INC}"
    "${PROJECT_SOURCE_DIR}/Source/System/Linux"
  )
elseif (WINDOWS)
  set(INC
    "${INC}"
    "${PROJECT_SOURCE_DIR}/Source/System/Windows"
  )
else ()
  # TODO: implement
endif()


#-------------------------------------------------------------------------------
# Source Files
#-------------------------------------------------------------------------------
set(SRC
  "${CMAKE_CURRENT_SOURCE_DIR}/Source/CodeSheriff.cpp"
  "${CMAKE_CURRENT_SOURCE_DIR}/Source/Configuration/CConfiguration.cpp"
  "${CMAKE_CURRENT_SOURCE_DIR}/Source/Configuration/CKey.cpp"
  "${CMAKE_CURRENT_SOURCE_DIR}/Source/Configuration/CParameter.cpp"
  "${CMAKE_CURRENT_SOURCE_DIR}/Source/Configuration/CParameterList.cpp"
  "${CMAKE_CURRENT_SOURCE_DIR}/Source/Interfaces/IPlugin.cpp"
  "${CMAKE_CURRENT_SOURCE_DIR}/Source/Interfaces/IPluginInput.cpp"
  "${CMAKE_CURRENT_SOURCE_DIR}/Source/System/CFile.cpp"
)

if (UNIX)
  set(SRC
    "${SRC}"
    "${CMAKE_CURRENT_SOURCE_DIR}/Source/System/Linux/CMutex.cpp"
  )
elseif (WINDOWS)
  set(SRC
    "${SRC}"
    "${CMAKE_CURRENT_SOURCE_DIR}/Source/System/Windows/CMutex.cpp"
  )
else ()
  # TODO: implement
endif()


#-------------------------------------------------------------------------------
# Sub-directories
# Note, include other CMakeLists.txt, stops and parses immediately.
#-------------------------------------------------------------------------------
add_subdirectory("${PROJECT_SOURCE_DIR}/Source/External/Pluma")
add_subdirectory("${PROJECT_SOURCE_DIR}/Source/Plugins/Input")
add_subdirectory("${PROJECT_SOURCE_DIR}/Source/Plugins/Process/Whitespace")


#-------------------------------------------------------------------------------
# Libraries (Linker)
#-------------------------------------------------------------------------------
# link_directories(
#   "${CMAKE_CURRENT_SOURCE_DIR}/Source/External/Pluma"
# )


#-------------------------------------------------------------------------------
# Display Build Variables
#-------------------------------------------------------------------------------
message(STATUS )
message(STATUS )
message(STATUS "BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}" )
message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}" )
message(STATUS "CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}" )
message(STATUS "CMAKE_MODULE_PATH = ${CMAKE_MODULE_PATH}" )
message(STATUS "${PROJECT_NAME}_DEPENDS = \"${${PROJECT_NAME}_DEPENDS}\"" )
message(STATUS "BUILD_WITH = \"${BUILD_WITH}\"" )
message(STATUS "INSTALL_DOC = ${INSTALL_DOC}" )
message(STATUS "SRC = ${SRC}" )
message(STATUS "INC = ${INC}" )
message(STATUS "Change a value with: cmake -D<Variable>=<Value>" )
message(STATUS )
message(STATUS )


#-------------------------------------------------------------------------------
# Generate Configuration Header
# This will generate a configuration header file that allows us to pass
# necessary CMake settings to the source code.
#-------------------------------------------------------------------------------
configure_file(
  "${PROJECT_SOURCE_DIR}/Source/System/Config.h.in"
  "${PROJECT_BINARY_DIR}/Source/System/Config.h"
)


#-------------------------------------------------------------------------------
# Build Program
#-------------------------------------------------------------------------------
include_directories(${INC})
add_executable(code-sheriff ${SRC})


#-------------------------------------------------------------------------------
# Link Libraries
#-------------------------------------------------------------------------------
# target_link_libraries (code-sheriff ${CMAKE_CURRENT_SOURCE_DIR}/Source/External/Pluma)
target_link_libraries (code-sheriff Pluma)
if(UNIX AND NOT APPLE)
  target_link_libraries(code-sheriff rt)
  target_link_libraries(code-sheriff dl)
endif()

if(Boost_FOUND)
  target_link_libraries(code-sheriff ${Boost_LIBRARIES})
endif()
